//
//  welcomeViewController.swift
//  weibo
//
//  Created by David on 15/11/27.
//  Copyright © 2015年 DV. All rights reserved.
//

import UIKit
import SDWebImage

class welcomeViewController: UIViewController {
    private var iconConstraint : NSLayoutConstraint?
    private var backView = UIImageView(image: UIImage(named: "ad_background"))
    private var icon : UIImageView = {
        let image = UIImageView()
        image.image = UIImage(named: "avatar_default_big")
        image.layer.cornerRadius = 45
        //裁剪
        image.layer.masksToBounds = true
        
        
        
        return image
    }()
    private var label : UILabel = {
        let label = UILabel()
        label.text = "欢迎回来"
        label.textColor = UIColor.blackColor()
        return label
    }()
    
    override func viewDidLoad() {
        icon.sd_setImageWithURL(UserAccountViewModel.sharedTools.avatarURL)
        
        view.addSubview(backView)
        view.addSubview(icon)
        view.addSubview(label)
        
        icon.translatesAutoresizingMaskIntoConstraints = false
        label.translatesAutoresizingMaskIntoConstraints = false
        
        view.addConstraint(NSLayoutConstraint(item: icon, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 90))
        view.addConstraint(NSLayoutConstraint(item: icon, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 90))
        view.addConstraint(NSLayoutConstraint(item: icon, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        view.addConstraint(NSLayoutConstraint(item: icon, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: -200))
        iconConstraint = view.constraints.last
        
        view.addConstraint(NSLayoutConstraint(item: label, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        view.addConstraint(NSLayoutConstraint(item: label, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 250))
    }
    
    override func viewDidAppear(animated: Bool) {
        label.alpha = 0
        iconConstraint?.constant = -UIScreen.mainScreen().bounds.height - iconConstraint!.constant
        UIView.animateWithDuration(1.5, delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 12, options: [], animations: { () -> Void in
            self.view.layoutIfNeeded()
            }) { (_) -> Void in
                UIView.animateWithDuration(1, animations: { () -> Void in
                    self.label.alpha = 1
                    }, completion: { (_) -> Void in
                            NSNotificationCenter.defaultCenter().postNotificationName(DVSwitchMainInterfaceNotification, object: "main")
                        
                })

        }
    }
}
